
function SejourCodeDirective (Sejour) {
      return {
        restrict: 'A',
        require: 'ngModel', 
        link: function(scope, elem, attrs, ctrl) {

          var val = elem.val();                  
          var _timeoutAction = null;

          ctrl.$parsers.unshift(function(value) {
            var valid = true;
            var scSejour = new Sejour();
                  
              if(typeof value != 'undefined' && value != '') {

                  if( _timeoutAction != null) clearTimeout( _timeoutAction );

                  _timeoutAction = setTimeout(function() {
                    scSejour.verifierSiLeCodeSejourEstPris( {'code' : value } ).then( function(e) {
                      ctrl.$setValidity('iscodevalid', e.data.codedispo == true );
                    });
                  }, 500);
              }                  
              
              return valid ? value : undefined;
          });
          
          ctrl.$formatters.unshift(function(value) {
              
              if(typeof value != 'undefined' && value != ''){
                      ctrl.$setValidity('iscodevalid', true);
              }
              
              return value;
          });

        }
      }
};

   